﻿using Castle.Core.Logging;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Org.BouncyCastle.Asn1.Ocsp;
using Traceability.Register.Application.Commonds.TraceabilityManagementCommonds;
using Traceability.Register.Domain;
using Traceability.Register.ErrorCode.Result;
using Traceability.Register.Infrastructrue;

namespace Traceability.Register.Writer.API.Controllers
{
    /// <summary>
    /// 溯源管理控制器——写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InsertTraceabilityManagementsController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        private readonly IBaseRepository<Goods> baseRepository;
        private readonly IBaseRepository<Traceabilitys> repository;
        private readonly ILogger<InsertTraceabilityManagementsController> logger;

        public InsertTraceabilityManagementsController(IMediator mediator, IBaseRepository<Goods> baseRepository, IBaseRepository<Traceabilitys> repository, ILogger<InsertTraceabilityManagementsController> logger)
        {
            this.mediator = mediator;
            this.baseRepository = baseRepository;
            this.repository = repository;
            this.logger = logger;
        }



        /// <summary>
        /// 插入溯源管理
        /// </summary>
        /// <param name="param">请求</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<ApiResult> InsertTraceability(InsertTraceabilityManagementCommond param)
        {
            return await mediator.Send(param);
        }
        /// <summary>
        /// 删除溯源管理
        /// </summary>
        /// <param name="param">请求</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public async Task<ApiResult> DelTraceability([FromQuery]DelQRCodeRequestCommond param)
        {
            return await mediator.Send(param);
        }
        /// <summary>
        /// 修改溯源管理
        /// </summary>
        /// <param name="param">请求</param>
        /// <returns>返回任务</returns>
        [HttpPut]
        public async Task<ApiResult> UpdateTraceability([FromQuery]UpdateQRCodeRequestCommond param)
        {
            return await mediator.Send(param);
        }
        /// <summary>
        /// 查询溯源管理
        /// </summary>
        /// <param name="param">请求</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult> GetTraceabilitysFindCommond([FromQuery]TraceabilitysFindCommond param)
        {
            return await mediator.Send(param);
        }
        /// <summary>
        /// 查询养殖管理
        /// </summary>
        /// <param name="param">请求</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult> GetBreedingsFindCommond([FromQuery]BreedingsFindCommond param)
        {
            return await mediator.Send(param);
        }
        /// <summary>
        /// 免疫信息反填
        /// </summary>
        /// <param name="param">请求</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult> GetImunitys([FromQuery]ImunitysFindCommond param)
        {
            return await mediator.Send(param);
        }
        /// <summary>
        /// 公司信息反填
        /// </summary>
        /// <param name="param">请求</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult> GetSlaughterEnterprise([FromQuery]SlaughterEnterpriseFindCommond param)
        {
            return await mediator.Send(param);
        }
        /// <summary>
        /// 添加养殖场信息
        /// </summary>
        /// <param name="param">请求</param>
        /// <returns>返回结果</returns>
        [HttpPost]
        public async Task<ApiResult> InsertBreedings(InsertBreedingsCommond param)
        {
            return await mediator.Send(param);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> UpdateBreedings(UpdateBreedingsCommond param)
        {
            return await mediator.Send(param);
        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetGenerateQRCodeRequest(GenerateQRCodeRequestCommond param)
        {
            return await mediator.Send(param);
        }
        [HttpGet]
        public async Task<IActionResult> GetQrCodeText(int traceabilityId,int BreedingSum)
        {
            var data = await baseRepository.GetAllAsync();
            var entity = await repository.GetAllAsync();
            var goodsInfo = (from a in data
                             join b in entity
            on a.GoodId equals b.GoodId
                             where b.TraceabilityId == traceabilityId
                             select new
                             {
                                 GoodName=a.GoodName,
                                 ProductionBatch = b.ProductionBatch
                             }).FirstOrDefault();

            if (goodsInfo == null)
            {
                logger.LogError($"未找到对应的溯源信息，TraceabilityId: {traceabilityId}");
                return new NotFoundResult();
            }
            string textContent = $"商品名称: {goodsInfo.GoodName}, 批次: {goodsInfo.ProductionBatch}, 数量: {BreedingSum}";
            return Content(textContent, "text/plain");
        }
    }
}
